package com.scrapy.http.get;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.scrapy.http.body.HttpBody;

/**
 * 一个url封装的对象
 * @author wanter
 *
 */
public class HttpClient {
	/**
	 * 要抓取的地址
	 */
	private String urls;
	/**
	 * 封装的页面
	 */
	private HttpBody httpBody;

	public HttpClient(String urls) {
		this.httpBody = this.post(urls);
	}

	public String getUrls() {
		return urls;
	}

	public void setUrls(String urls) {
		this.urls = urls;
	}

	public HttpBody getHttpBody() {
		return this.httpBody;
	}
	
	private HttpBody post(String url){
		URLConnection connection = null;
		BufferedReader reader = null;
		URL urls = null;
		this.httpBody = new HttpBody();
		try {
			urls = new URL(url);
			connection = urls.openConnection();
			/**
			 * 设置浏览器发送的头文件
			 */
			connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");
			String line = "";
			String temp = "";
			reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			while((line = reader.readLine())!=null){
				temp = temp + line.trim();
			}
			httpBody.setCountxt(temp);
			httpBody.setDate(new Date());
			httpBody.setUrl(connection.getURL());
			
		} catch (IOException e) {
			System.err.println("请检查你传入的url地址");
			e.printStackTrace();
		}
		return httpBody;
	}

	
}
